﻿using static System.Runtime.InteropServices.JavaScript.JSType;
using System.Data.SqlClient;
using yunbo.Model.PPMS;
using yunbo.Model.PPM;
using Dapper;

namespace yunbo.Repositorys.PPMRepositorys
{
    public class Mes_gdrwRepository
    {
        public string data = "Data Source=121.37.93.71;Initial Catalog=FanXingMES;User ID=sa;Password=geili@2025;";


        public List<Mes_gdrw> GetMes_gdrw()
        {
            using (SqlConnection connection = new SqlConnection(data))
            {
                string sql = "select* from mes_gdrw";
                List<Mes_gdrw> Mes_gdrw = connection.Query<Mes_gdrw>(sql).ToList();
                return Mes_gdrw;
            }
        }
        public int AddMes_gdrw(ADDgdrw scgd)
        {
            // 新的 SQL 插入语句
            string sql = @"INSERT INTO [dbo].[mes_gdrw] (gdrw_state, gdrw_yxj, gdrw_code, gdrw_name, cp_code, cp_name, cp_xh, cp_lx,cp_fl, dw_code, dw_name,pp_code, pp_name,  ks_code,ks_name,ht_code, bom_code, gdrw_jhsl, gdrw_xqrq, gdrw_jhkgsj, gdrw_jhwgsj,gdrw_scwz,gdrw_scbz,gy_code,gy_name,gdrw_bz,scgd_code, create_by, create_time,gdrw_jd,gdrw_fzr,gd_tcsj)
                   VALUES (58, @gdrw_yxj, @gdrw_code, @gdrw_name, @cp_code, @cp_name, @cp_xh, @cp_lx,@cp_fl, @dw_code, @dw_name,@pp_code, @pp_name, @ks_code,@ks_name ,@ht_code,@bom_code, @gdrw_jhsl, @gdrw_xqrq,@gdrw_jhkgsj,@gdrw_jhwgsj,@gdrw_scwz,@gdrw_scbz,@gy_code,@gy_name,@gdrw_bz,@scgd_code, @create_by, GETDATE(),@gdrw_jd,@gdrw_fzr,GETDATE())";
            try
            {
                using (SqlConnection connection = new SqlConnection(data))
                {
                    connection.Open();
                    // 调整参数列表以匹配新的 SQL 语句
                    var parameters = new
                    {
                        gdrw_yxj = scgd.gdrw_yxj,
                        gdrw_code = scgd.gdrw_code,
                        gdrw_name = scgd.gdrw_name,
                        cp_code = scgd.cp_code,
                        cp_name = scgd.cp_name,
                        cp_xh = scgd.cp_xh,
                        cp_lx = scgd.cp_lx,
                        cp_fl = scgd.cp_fl,
                        dw_code = scgd.dw_code,
                        dw_name = scgd.dw_name,
                        ks_code = scgd.ks_code,
                        ks_name = scgd.ks_name,
                        pp_code = scgd.pp_code,
                        pp_name = scgd.pp_name,
                        bom_code = scgd.bom_code,
                        gdrw_jhsl = scgd.gdrw_jhsl,
                        gdrw_xqrq = scgd.gdrw_xqrq,
                        gdrw_jhkgsj = scgd.gdrw_jhkgsj,
                        gdrw_jhwgsj = scgd.gdrw_jhwgsj,
                        gdrw_bz = scgd.gdrw_bz,
                        gy_name=scgd.gy_name,
                        gy_code=scgd.gy_code,
                        gdrw_scwz = scgd.gdrw_scwz,
                        gdrw_scbz=scgd.gdrw_scbz,
                        scgd_code = scgd.scgd_code,
                        create_by = scgd.create_by,
                        ht_code = scgd.ht_code,
                        gdrw_jd=scgd.gdrw_jd,
                        gdrw_fzr=scgd.gdrw_fzr,
                    };

                    var affectedRows = connection.Execute(sql, parameters);
                    return affectedRows;
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
                throw;
            }
        }
        /// <summary>
        /// 开工
        /// </summary>
        /// <param name="kgsj"></param>
        /// <param name="gdrw_code"></param>
        /// <returns></returns>
        public int Updatekgsj(string gdrw_code, int state, int update_by)
        {
            using (SqlConnection connection = new SqlConnection(data))
            {
                string sql = "UPDATE mes_gdrw SET  gdrw_state = @state, update_by = @update_by, update_time = GETDATE(),gdrw_sjkgsj = GETDATE() WHERE gdrw_code = @gdrw_code";
            
                // 使用参数化查询来防止 SQL 注入

                // 创建命令对象并设置参数
                using (SqlCommand command = new SqlCommand(sql, connection))
                {
                    command.Parameters.AddWithValue("@gdrw_code", gdrw_code);
                    command.Parameters.AddWithValue("@update_by", update_by);
                    command.Parameters.AddWithValue("@state", state);
                    // 打开连接并执行命令
                    connection.Open();
                    int affectedRows = command.ExecuteNonQuery();

                    // 返回受影响的行数
                    return affectedRows;
                }
            }
        }
        /// <summary>
        /// 完工
        /// </summary>
        /// <param name="kgsj"></param>
        /// <param name="gdrw_code"></param>
        /// <returns></returns>
        public int Updatewgsj( string gdrw_code, int state, int update_by)
        {
            using (SqlConnection connection = new SqlConnection(data))
            {
                string sql = "UPDATE mes_gdrw SET  gdrw_state = @state, update_by = @update_by, update_time = GETDATE(),gdrw_sjwgsj = GETDATE() WHERE gdrw_code = @gdrw_code";

                // 使用参数化查询来防止 SQL 注入

                // 创建命令对象并设置参数
                using (SqlCommand command = new SqlCommand(sql, connection))
                {
                    command.Parameters.AddWithValue("@gdrw_code", gdrw_code);
                    command.Parameters.AddWithValue("@update_by", update_by);
                    command.Parameters.AddWithValue("@state", state);
                    // 打开连接并执行命令
                    connection.Open();
                    int affectedRows = command.ExecuteNonQuery();

                    // 返回受影响的行数
                    return affectedRows;
                }
            }
        }
        /// <summary>
        /// 修改状态
        /// </summary>
        /// <param name="code"></param>
        /// <param name="state"></param>
        /// <param name="update_by"></param>
        /// <returns></returns>
        public int UpdateStatus(string scgd_code, int state, int update_by, string gdrw_code)
        {
            using (SqlConnection connection = new SqlConnection(data))
            {
                string sql = "";
                if (scgd_code == "1")
                {
                    sql = "UPDATE mes_gdrw SET gdrw_state = @state, update_by = @update_by, update_time = GETDATE() WHERE gdrw_code = @gdrw_code";
                }
                else
                {
                    sql = "UPDATE mes_gdrw SET gdrw_state = @state, update_by = @update_by, update_time = GETDATE() WHERE gd_code = @scgd_code";
                }
                // 使用参数化查询来防止 SQL 注入

                // 创建命令对象并设置参数
                using (SqlCommand command = new SqlCommand(sql, connection))
                {
                    command.Parameters.AddWithValue("@gdrw_code", gdrw_code);
                    command.Parameters.AddWithValue("@scgd_code", scgd_code);
                    command.Parameters.AddWithValue("@update_by", update_by);
                    command.Parameters.AddWithValue("@state", state); // 注意：如果 cp_code 是数值类型，则不应该加引号

                    // 打开连接并执行命令
                    connection.Open();
                    int affectedRows = command.ExecuteNonQuery();

                    // 返回受影响的行数
                    return affectedRows;
                }
            }
        }
        /// <summary>
        /// 报工
        /// </summary>
        /// <param name="scgd_code"></param>
        /// <param name="shu"></param>
        /// <param name="gdjd"></param>
        /// <returns></returns>
        public int BaoGong(string scgd_code,int shu,int gdjd) 
        {
            return 1;
        }
        /// <summary>
        /// 报工
        /// </summary>
        /// <param name="scgd_code"></param>
        /// <param name="shu"></param>
        /// <param name="gdjd"></param>
        /// <returns></returns>
        public int WanGong(string code, int shu)
        {
            using (SqlConnection connection = new SqlConnection(data))
            {
                string sql = $"update mes_gdrw set gdrw_jd=100,gdrw_sjscsl={shu} where gdrw_code='{code}'";
                int rs = connection.Execute(sql);
                return rs;
            }
            
        }
    }
}
